package com.yanboo.generator.config;

import cn.hutool.db.dialect.DialectName;
import cn.hutool.db.dialect.impl.MysqlDialect;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceBuilder;
import com.yanboo.generator.service.DbDriverService;
import com.yanboo.generator.service.DriverLocalStorageService;
import com.yanboo.generator.service.ProjectInfoService;
import com.yanboo.generator.service.dto.DbDriverDto;
import com.yanboo.generator.service.dto.ProjectInfoDto;
import com.yanboo.generator.service.dto.ProjectInfoQueryCriteria;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.DefaultJpaDialect;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
@ConditionalOnBean(DefaultDataSourceProperties.class)
public class DataSourceAutoInject{
    @Autowired
    DefaultDataSourceProperties dataSourceProperties;

    @Bean
    public DynamicRoutingDataSource dynamicRoutingDataSource(DataSourceFactory factory) {
        DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();
        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource(factory);
        Map<Object, Object> dataSourceHashMap = new ConcurrentHashMap<>();
        dynamicRoutingDataSource.setTargetDataSources(dataSourceHashMap);
        dynamicRoutingDataSource.setDefaultTargetDataSource(dataSource);
        return dynamicRoutingDataSource;
    }
}
